﻿; (function (window, undefined, $) {

    $(document).ready(function () {

        var gridopt = {
            url: options.listUrl,
            colModel: [
                    { display: '项目编号', name: 'ProjectNo', width: "5%", sortable: true, hide: true, align: 'left', iskey: true },
                    { display: 'pid', name: 'pid', hide: true, width: "15%", sortable: false, align: 'left' },
                    { display: '项目名称', name: 'ProjectName', width: "15%", sortable: false, align: 'left' },
                    { display: '借款总额', name: 'TotalLoan', width: "15%", sortable: false, align: 'left', process:processTotalLoan },
                    { display: '还款编号', name: 'RepaymentPlanNO', width: "10%", sortable: false, align: 'left' },
                    { display: '还款日期', name: 'RepaymentPlanDate', width: "12%", sortable: false, align: 'left' },
                    { display: '当期本息', name: 'CurrentPrincipal', width: "12%", sortable: false, align: 'left', process: processTotalLoan },
                    { display: '剩余本金', name: 'RemainingPrincipal', width: "13%", sortable: false, align: 'left', process: processTotalLoan },
                    { display: '还款状态', name: 'statusName', width: "8%", sortable: false, align: 'left' }//,
                    //{ display: '操作', name: 'ID', width: "10%", sortable: false, align: 'center', process: processOp }
            ],
            sortname: "ID",
            sortorder: "ASC",
            title: false,
            rp: 15,
            usepager: true,
            showcheckbox: false
        };
        var xjgrid = new xjGrid("gridlist", gridopt);
        //function processOp(value, cell) {
        //    var ops = [];
        //    if (cell[8]=="已还款") {
        //        ops.push("&nbsp;&nbsp;已还款");

        //    } else {
        //        ops.push("&nbsp;&nbsp;<a title='还款' class='abtn' href='javascript:;'  onclick=\"util.RepayPres('", value, "','", cell[1], "')\"><i class='fa fa-trash-o' ></i>还款</a>");

        //    }
        //    return ops.join("");
        //}

        util.RepayPres = function (id,pid) {
            $.post(options.Repayment, { id: id, projectid: pid, name: $('#RepaymentUser').val() },
                  function (res) {
                      if (res > 0) {
                          if (res == 10001) {
                              alert('还没到还款时间！');
                          }
                          else if (res == 10003) {
                              alert('已还款！');
                          }
                          else {
                              window.location.href = "/Project/RepaymentList.html";
                          }
                      }
                      else {
                          alert("操作失败！");
                      }
                  },
                  "json"
            );
        };

    });

    function processTotalLoan(value) {
        return formatNum(value);
    }

    function formatNum(v) {
        if (isNaN(v)) {
            return v;
        }
        v = (Math.round((v - 0) * 100)) / 100;
        v = (v == Math.floor(v)) ? v + ".00" : ((v * 10 == Math.floor(v * 10)) ? v
                + "0" : v);
        v = String(v);
        var ps = v.split('.');
        var whole = ps[0];
        var sub = ps[1] ? '.' + ps[1] : '.00';
        var r = /(\d+)(\d{3})/;
        while (r.test(whole)) {
            whole = whole.replace(r, '$1' + ',' + '$2');
        }
        v = whole + sub;

        return v;
    }

})(window, undefined, jQuery);